﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using com.stockstar.baseinfo.datacontract;

using System.Linq;
using Com.Stockstar.Quote.Model.Quote;
using Com.Stockstar.Quote.Common.Quote;
/// <summary>
/// 此类是关于基金的公共操作，包括基金代码表
/// </summary>
public class FundCommon
{
    private static List<string> fundCodeList = new List<string>();
    private static List<FundCode> allFundCodes = new List<FundCode>();
    private static string cachekey = "All_Fund_Codes";
    private static string fundcodecachekey = "fundCodeType";
    /// <summary>
    /// 基金代码表实例
    /// </summary>
    public static List<string> FundCodesListInstance
    {
        get{
            if (CacheManager.GetCache(cachekey) != null)
            {
                return CacheManager.GetCache(cachekey) as List<string>;
            }
            if (fundCodeList == null || fundCodeList.Count == 0)
            {
                fundCodeList = getFundCodes();
                if (fundCodeList != null && fundCodeList.Count > 0)
                {
                    CacheManager.AddCache(cachekey, fundCodeList, 24 * 60); //缓存保存一天        
                }
            }
            return fundCodeList;
        }

    }

    public static List<FundCode> AllFundCodesList
    {
        get
        {
            if (CacheManager.GetCache(fundcodecachekey) != null)
            {
                return CacheManager.GetCache(fundcodecachekey) as List<FundCode>;
            }
            if (allFundCodes == null || allFundCodes.Count == 0)
            {
                allFundCodes = getFundCodesObj();
                if (allFundCodes != null && allFundCodes.Count > 0)
                {
                    CacheManager.AddCache(fundcodecachekey, allFundCodes, 24 * 60); //缓存保存一天        
                }
            }
            return allFundCodes;
        }

    }
    /// <summary>
    /// 生成基金代码表,从SG接口中取代码表数据
    /// </summary>
    /// <param name="fundCode"></param>
    /// <param name="dateString"></param>
    /// <returns></returns>
    private static List<string> getFundCodes()
    {
        List<string> fundList = new List<string>();
        try
        {
            List<SecurityCode> allfundcode = AllCodeSingleton.Instance.CodeList.Where(x => x.SecurtyType >= 500 && x.SecurtyType < 600).ToList();
            foreach (var code in allfundcode)
            {
                fundList.Add(code.Code);
            }
        }
        catch (Exception ex) {
        }
        return fundList;
    }

    /// <summary>
    /// 生成基金代码表,从SG接口中取代码表数据
    /// </summary>
    /// <param name="fundCode"></param>
    /// <param name="dateString"></param>
    /// <returns></returns>
    private static List<FundCode> getFundCodesObj()
    {
        List<FundCode> fundList = new List<FundCode>();
        try
        {
            List<SecurityCode> allfundcode = AllCodeSingleton.Instance.CodeList.Where(x => x.SecurtyType >= 500 && x.SecurtyType < 600).ToList();
            foreach (var code in allfundcode)
            {

                FundCode fundcode = new FundCode();
                fundcode.Code = code.Code;
                fundcode.SimpleName = code.Name;
                fundcode.MarketType = (int)code.MarketType;
                fundcode.SecurityType= (int)code.SecurtyType;

                fundList.Add(fundcode);
            }
        }
        catch (Exception ex)
        {
        }
        return fundList;
    }

}
